package controller;

import model.Student;
import model.Teacher;
import model.User;
import config.Consts;

/**
 * Created by BaiYunfei on 2017/6/14.
 */
public class IndexController extends BaseController{

    public void index(){
    	User user = User.dao.findById(getSession().getAttribute(Consts.SESSION_USER_ID));

        switch (user.getRole()) {
		case 1:
			Student stu = Student.dao.findFirst("SELECT * FROM Student WHERE [ID]=?",user.getId());
			setAttr("name",stu.get("Student_name"));
			break;
		case 2:
			Teacher tea = Teacher.dao.findFirst("SELECT * FROM Teacher WHERE [ID]=?",user.getId());
			setAttr("name",tea.get("Teacher_name"));
			break;
		default:
			setAttr("name",user.getUsername());
			break;
		}
        
        render("index.html");
    }

    /**
     * 登陆页面
     */
    public void loginPage(){
        render("login.html");
    }

    /**
     * 登陆
     */
    public void login(){
        User user = getModel(User.class,"");
        if((user = user.login()) != null){
            setSessionAttr(Consts.SESSION_USER_ID, user.getId());
            setSessionAttr(Consts.SESSION_USER_ROLE,user.getRole());
            
            setAttr("status", true);
        }else{
            setAttr("status", false);
        }
        renderJson();
    }
    
    public void logout(){
    	removeSessionAttr(Consts.SESSION_USER_ID);
    	removeSessionAttr(Consts.SESSION_USER_ROLE);
    	redirect("/loginPage");
    }

    public void tab(){
        renderJsp("tab.jsp");
    }

}
